//
//  GuestViewController.m
//  QRMaster
//
//  Created by myApple on 15/6/24.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "GuestViewController.h"
#import "GesturePasswordController.h"
#import "GuestureCell.h"

@interface GuestViewController ()
{
    UITableView * guestureTabelView;
    NSArray * guestureArray;
    NSUserDefaults * userInfo;
    UIButton * fixGuestureBtn;
    UISwitch * guestureSwitch;
    
    //多语言
    UILabel * titleNameLabel;
    UILabel * guestLabel;
    UILabel * fixGuestLabel;
}

@end

@implementation GuestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    userInfo = [NSUserDefaults standardUserDefaults];
    guestureArray = @[NSLocalizedString(@"openGuesturePWD", nil),NSLocalizedString(@"modifyGesture", nil)];
    [self prepareMainView];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationItem.title=NSLocalizedString(@"setGesture", nil);
    guestureArray = @[NSLocalizedString(@"openGuesturePWD", nil),NSLocalizedString(@"modifyGesture", nil)];
    titleNameLabel.text = NSLocalizedString(@"setGesture", nil);
    guestLabel.text = NSLocalizedString(@"openGuesturePWD", nil);
    fixGuestLabel.text = NSLocalizedString(@"modifyGesture", nil);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --手势界面
-(void)prepareMainView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 10;
    float y = 30;
    float width = ScreenWidth - 2 * x;
    float height = 50;
    
    //开启手势密码
    UIButton * guestPWDBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [guestPWDBtn setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:guestPWDBtn];
    
    x = 40;
    y = 15;
    height = 20;
    width = 160;
    guestLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    guestLabel.text = NSLocalizedString(@"openGuesturePWD", nil);
    guestLabel.font = [UIFont systemFontOfSize:16.0];
    [guestPWDBtn addSubview:guestLabel];
    
    guestureSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(guestPWDBtn.frame.size.width - 60, 10, 40, 30)];
    [guestureSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    [guestPWDBtn addSubview: guestureSwitch];
    
    //修改手势密码
    x = guestPWDBtn.frame.origin.x;
    y = guestPWDBtn.frame.origin.y + guestPWDBtn.frame.size.height + 5;
    width = guestPWDBtn.frame.size.width;
    height = guestPWDBtn.frame.size.height;
    fixGuestureBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [fixGuestureBtn setBackgroundColor:[UIColor whiteColor]];
    if ([[userInfo objectForKey:@"gesture"] isEqualToString:@"1"]) {
         fixGuestureBtn.hidden = NO;
        guestureSwitch.on = YES;
    }
    else
    {
        fixGuestureBtn.hidden = YES;
        guestureSwitch.on = NO;
    }
   
    [fixGuestureBtn addTarget:self action:@selector(fixGuestureClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fixGuestureBtn];
    
    x = 40;
    y = 15;
    height = 20;
    width = 120;
    fixGuestLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    fixGuestLabel.text = NSLocalizedString(@"modifyGesture", nil);
    fixGuestLabel.font = [UIFont systemFontOfSize:16.0];
    [fixGuestureBtn addSubview:fixGuestLabel];
}


//修改手势密码
-(void)fixGuestureClick:(UIButton *)btn
{
    GesturePasswordController * guvc = [[GesturePasswordController alloc]init];
    guvc.comein = @"reset";
    [self.navigationController pushViewController:guvc animated:YES];
    [userInfo setValue:@"1" forKey:@"gesture"];
    [userInfo synchronize];
}

//开启或者关闭手势密码
-(void)switchAction:(UISwitch *)switchStatus
{
    if (switchStatus.on) {
        GesturePasswordController * guvc = [[GesturePasswordController alloc]init];
        if (![guvc exist]) {
            guvc.comein = @"reset";
            [self.navigationController pushViewController:guvc animated:YES];
            guvc.fixGuesture = ^(NSString * str)
            {
                if ([str isEqualToString:@"yes"]) {
                    fixGuestureBtn.hidden = NO;//设置了手势密码
                }
                else
                {
                    fixGuestureBtn.hidden = YES;//直接退出没有设置手势密码
                    switchStatus.on = NO;
                }
            };
        }
        else
        {
             //存在手势密码
            fixGuestureBtn.hidden = NO;
            [userInfo setValue:@"1" forKey:@"gesture"];
            [userInfo synchronize];
        }
        
    }
    else
    {
         //手势密码开关没有打开
        [userInfo setValue:@"0" forKey:@"gesture"];
        fixGuestureBtn.hidden = YES;
        [userInfo synchronize];
    }
    
    
    
}


@end
